package com.pzsh.controller;

import com.alibaba.fastjson.JSONObject;
import com.pzsh.entity.ResponseWrapper;
import com.pzsh.entity.vo.*;
import com.pzsh.enums.ErrorCodeEnum;
import com.pzsh.exception.ApiException;
import com.pzsh.jwt.CurrentManager;
import com.pzsh.service.IGoodsService;
import com.pzsh.service.IOrderService;
import com.pzsh.swagger.ApiJsonObject;
import com.pzsh.swagger.ApiJsonProperty;
import com.pzsh.util.Asserts;
import com.pzsh.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: 用户接口【前端】
 * @description: 用户
 * @author: gxl
 * @create:
 **/
@Slf4j
@Api(value = "goods", tags = "商品")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IOrderService orderService;

    //测试ok
    @RequestMapping(value = "/grade/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取升级套餐产品列表", response = List.class)
    public ResponseWrapper findListOfGrade(@ApiJsonObject(name = "gradefindListParams", value = {}) @RequestBody JSONObject jsonParam) {
        try {
            List<GradeVo> list = goodsService.findGradeList();
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]获取升级套餐产品列表失败，参数:%s, 错误信息:%s", "jsonParam.toJSONString()", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取升级套餐产品列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @RequestMapping(value = "/goods/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取商品列表", response = List.class)
    public ResponseWrapper findListOfGoods(@ApiJsonObject(name = "goodsfindListParams", value = {}) @RequestBody JSONObject jsonParam) {
        try {
            List<GoodsVo> list = goodsService.findGoodsList();
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]获取升级套餐产品列表失败，参数:%s, 错误信息:%s", "jsonParam.toJSONString()", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取升级套餐产品列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/goods/getInfoById", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取商品图文详情", response = List.class)
    public ResponseWrapper getInfoById(@ApiJsonObject(name = "getInfoByIdParams", value = {
            @ApiJsonProperty(key = "goodsId", value = "商品ID")
    }) @RequestBody JSONObject jsonParam) {

        try {
            Integer goodsId = jsonParam.getInteger("goodsId");
            Asserts.notBlank(goodsId.toString(), "商品ID");
            List<GoodsInfoVo> list = goodsService.getInfoById(goodsId);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]获取升级套餐产品列表失败，参数:%s, 错误信息:%s", "jsonParam.toJSONString()", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取升级套餐产品列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/getIndexRotaion", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取首页轮播图片", response = List.class)
    public ResponseWrapper getIndexRotaion(@ApiJsonObject(name = "getIndexRotaionParams", value = {
    }) @RequestBody JSONObject jsonParam) {

        try {
            List<GoodsRotaionVo> list = goodsService.getIndexRotaion();
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]获取首页轮播图片失败，参数:%s, 错误信息:%s", "jsonParam.toJSONString()", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取首页轮播图片列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/goods/getRotaionById", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取商品轮播图片", response = List.class)
    public ResponseWrapper getRotaionById(@ApiJsonObject(name = "getRotaionByIdParams", value = {
            @ApiJsonProperty(key = "goodsId", value = "商品ID")
    }) @RequestBody JSONObject jsonParam) {

        try {
            Integer goodsId = jsonParam.getInteger("goodsId");
            Asserts.notBlank(goodsId.toString(), "商品ID");
            List<GoodsRotaionVo> list = goodsService.getRotaionById(goodsId);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[前端]获取商品轮播图片失败，参数:%s, 错误信息:%s", "jsonParam.toJSONString()", e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取商品轮播图片列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @RequestMapping(value = "/goods/getInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取商品详情", response = GoodsVo.class)
    public ResponseWrapper getGoodsInfo(
            @ApiJsonObject(name = "getGoodsInfoParams", value = {
                    @ApiJsonProperty(key = "goodsId", value = "商品ID")
            }) @RequestBody JSONObject jsonParam) {


        try {
            Integer goodsId = jsonParam.getInteger("goodsId");
            Asserts.notBlank(goodsId.toString(), "商品ID");
            GoodsVo goodsVo = goodsService.getGoodsInfo(goodsId);
            return ResponseWrapper.succeed(goodsVo);
        }catch (ApiException e){
            log.error(String.format("[前端]获取商品详情失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取商品详情"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @RequestMapping(value = "/grade/getInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取升级产品详情", response = GradeVo.class)
    public ResponseWrapper getGradeInfo(
            @ApiJsonObject(name = "getGradeInfoParams", value = {
                    @ApiJsonProperty(key = "gradeId", value = "升级产品ID")
            }) @RequestBody JSONObject jsonParam) {


        try {
            Integer gradeId = jsonParam.getInteger("gradeId");
            Asserts.notBlank(gradeId.toString(), "升级产品ID");
            GradeVo gradeVo = goodsService.getGradeInfo(gradeId);
            return ResponseWrapper.succeed(gradeVo);
        }catch (ApiException e){
            log.error(String.format("[前端]获取升级产品详情失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取升级产品详情"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @RequestMapping(value = "/grade/buy", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]购买升级产品", response = String.class)
    public ResponseWrapper buyGrade(
            @ApiJsonObject(name = "buyGradeParams", value = {
                    @ApiJsonProperty(key = "gradeId", value = "升级产品ID"),
                    @ApiJsonProperty(key = "receiver", value = "收货人"),
                    @ApiJsonProperty(key = "mobile", value = "手机号码"),
                    @ApiJsonProperty(key = "city", value = "省市区"),
                    @ApiJsonProperty(key = "address", value = "地址")
            }) @RequestBody JSONObject jsonParam) {


        try {
            Integer gradeId = jsonParam.getInteger("gradeId");
            String account = jsonParam.getString("account");
            String receiver = jsonParam.getString("receiver");
            String mobile = jsonParam.getString("mobile");
            String city = jsonParam.getString("city");
            String address = jsonParam.getString("address");
            Asserts.checkMobile(mobile, "手机号码");
            Asserts.notBlank(receiver,"收货人");
            Asserts.notBlank(city,"省市区");
            Asserts.notBlank(address,"地址");
            Asserts.notBlank(account, "用户名");
            Asserts.notBlank(gradeId.toString(), "升级产品ID");
            goodsService.buyGrade(account,gradeId,1,receiver,mobile,city,address);
            return ResponseWrapper.succeed();
        }catch (ApiException e){
            log.error(String.format("[前端]购买升级产品失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]购买升级产品失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @RequestMapping(value = "/goods/buy", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]购买商品", response = String.class)
    public ResponseWrapper buyGoods(
            @ApiJsonObject(name = "buyGoodsParams", value = {
                    @ApiJsonProperty(key = "goodsId", value = "商品ID"),
                    @ApiJsonProperty(key = "numb", value = "购买数量"),
                    @ApiJsonProperty(key = "receiver", value = "收货人"),
                    @ApiJsonProperty(key = "mobile", value = "手机号码"),
                    @ApiJsonProperty(key = "city", value = "省市区"),
                    @ApiJsonProperty(key = "address", value = "地址")
            }) @RequestBody JSONObject jsonParam) {


        try {
            Integer goodsId = jsonParam.getInteger("goodsId");
            String account = jsonParam.getString("account");
            Integer numb = jsonParam.getInteger("numb");
            String mobile = jsonParam.getString("mobile");
            String receiver = jsonParam.getString("receiver");
            String city = jsonParam.getString("city");
            String address = jsonParam.getString("address");

            Asserts.notBlank(goodsId.toString(), "商品ID");
            Asserts.naturalNumber(numb, "购买数量");
            Asserts.checkMobile(mobile, "手机号码");
            Asserts.notBlank(receiver,"收货人");
            Asserts.notBlank(city,"省市区");
            Asserts.notBlank(address,"地址");
            Asserts.notBlank(account, "用户名");

            goodsService.buyGoods(account,goodsId,numb,receiver,mobile,city,address);
            return ResponseWrapper.succeed();
        }catch (ApiException e){
            log.error(String.format("[前端]购买商品失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]购买商品失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
//        return ResponseWrapper.succeed();
    }


    //测试ok
    @CurrentManager
    @RequestMapping(value = "/goods/confirmOrder", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取商品确认详情", response = OrderVo.class)
    public ResponseWrapper confirmOrderGoods(
            @ApiJsonObject(name = "confirmOrderGoodsParams", value = {
                    @ApiJsonProperty(key = "goodsId", value = "商品ID"),
                    @ApiJsonProperty(key = "numb", value = "购买数量"),
                    @ApiJsonProperty(key = "receiver", value = "收货人"),
                    @ApiJsonProperty(key = "mobile", value = "手机号码"),
                    @ApiJsonProperty(key = "city", value = "省市区"),
                    @ApiJsonProperty(key = "address", value = "地址")
            }) @RequestBody JSONObject jsonParam) {

        try {
            Integer goodsId = jsonParam.getInteger("goodsId");
            String account = jsonParam.getString("account");
            Integer numb = jsonParam.getInteger("numb");
            String mobile = jsonParam.getString("mobile");
            String receiver = jsonParam.getString("receiver");
            String city = jsonParam.getString("city");
            String address = jsonParam.getString("address");

            Asserts.notBlank(goodsId.toString(), "商品ID");
            Asserts.naturalNumber(numb, "购买数量");
            Asserts.checkMobile(mobile, "手机号码");
            Asserts.notBlank(receiver,"收货人");
            Asserts.notBlank(city,"省市区");
            Asserts.notBlank(address,"地址");
            Asserts.notBlank(account, "用户名");
            OrderVo orderVo = goodsService.confirmOrderGoods(account,goodsId,numb,receiver,mobile,city,address);
            return ResponseWrapper.succeed(orderVo);
        }catch (ApiException e){
            log.error(String.format("[前端]获取商品确认详情失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取商品确认详情"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @CurrentManager
    @RequestMapping(value = "/grade/confirmOrder", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取升级产品确认详情", response = OrderVo.class)
    public ResponseWrapper confirmOrderGrade(
            @ApiJsonObject(name = "confirmOrderGradeParams", value = {
                    @ApiJsonProperty(key = "gradeId", value = "升级产品ID"),
                    @ApiJsonProperty(key = "receiver", value = "收货人"),
                    @ApiJsonProperty(key = "mobile", value = "手机号码"),
                    @ApiJsonProperty(key = "city", value = "省市区"),
                    @ApiJsonProperty(key = "address", value = "地址")
            }) @RequestBody JSONObject jsonParam) {

        try {
            Integer gradeId = jsonParam.getInteger("gradeId");
            String account = jsonParam.getString("account");
            String receiver = jsonParam.getString("receiver");
            String mobile = jsonParam.getString("mobile");
            String city = jsonParam.getString("city");
            String address = jsonParam.getString("address");
            Asserts.checkMobile(mobile, "手机号码");
            Asserts.notBlank(receiver,"收货人");
            Asserts.notBlank(city,"省市区");
            Asserts.notBlank(address,"地址");
            Asserts.notBlank(account, "用户名");
            Asserts.notBlank(gradeId.toString(), "升级产品ID");
            OrderVo orderVo = goodsService.confirmOrderGrade(account,gradeId,receiver,mobile,city,address);
            return ResponseWrapper.succeed(orderVo);
        }catch (ApiException e){
            log.error(String.format("[前端]获取升级产品确认详情失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取升级产品确认详情"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }


    //测试ok
    @CurrentManager
    @RequestMapping(value = "/getOrdersByUserId", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取用户订单列表", response = List.class)
    public ResponseWrapper getOrdersByUserId(
            @ApiJsonObject(name = "getOrdersByUserIdParams", value = {
                    @ApiJsonProperty(key = "pageSize", value = "页码"),
                    @ApiJsonProperty(key = "rowSize", value = "行数"),
                    @ApiJsonProperty(key = "status", value = "订单状态")
            }) @RequestBody JSONObject jsonParam) {

        try {
            String account = jsonParam.getString("account");
            Asserts.notBlank(account, "用户名");
            Integer status = jsonParam.getInteger("status");
            Asserts.naturalNumber(status, "订单状态");
            Integer pageSize = jsonParam.getInteger("pageSize");
            Integer rowSize = jsonParam.getInteger("rowSize");
            if (pageSize==null|| "".equals(pageSize)){
                pageSize = 0;
            }
            if (rowSize == null || "".equals((rowSize))){
                rowSize = 10;
            }
            PageResult<List<OrderVo>> orderVos = orderService.getOrdersByUserId(account,status,pageSize,rowSize);
            return ResponseWrapper.succeed(orderVos.getTotal(), orderVos.getData());
        }catch (ApiException e){
            log.error(String.format("[前端]获取用户订单列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取用户订单列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @CurrentManager
    @RequestMapping(value = "/getOrderInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]获取订单详情", response = OrderVo.class)
    public ResponseWrapper getOrderInfo(
            @ApiJsonObject(name = "getOrderInfoParams", value = {
                    @ApiJsonProperty(key = "orderId", value = "订单id")
            }) @RequestBody JSONObject jsonParam) {

        try {
            String account = jsonParam.getString("account");
            Asserts.notBlank(account, "用户名");
            Integer orderId = jsonParam.getInteger("orderId");
            Asserts.naturalNumber(orderId, "订单ID");

            OrderVo  orderVos = orderService.getOrderInfo(account,orderId);
            return ResponseWrapper.succeed(orderVos);
        }catch (ApiException e){
            log.error(String.format("[前端]获取订单详情失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取订单详情列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @CurrentManager
    @RequestMapping(value = "/confirmReceive", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]确认收货", response = String.class)
    public ResponseWrapper confirmReceive(
            @ApiJsonObject(name = "confirmReceiveParams", value = {
                    @ApiJsonProperty(key = "orderId", value = "订单id")
            }) @RequestBody JSONObject jsonParam) {

        try {
            String account = jsonParam.getString("account");
            Asserts.notBlank(account, "用户名");
            Integer orderId = jsonParam.getInteger("orderId");
            Asserts.naturalNumber(orderId, "订单ID");

            orderService.confirmReceive(account,orderId);
            return ResponseWrapper.succeed();
        }catch (ApiException e){
            log.error(String.format("[前端]确认收货失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]确认收货失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    //测试ok
    @CurrentManager
    @RequestMapping(value = "/tranRecord", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]明细列表", response = List.class)
    public ResponseWrapper tranRecord(
            @ApiJsonObject(name = "tranRecordParams", value = {
                    @ApiJsonProperty(key = "pageSize", value = "页码"),
                    @ApiJsonProperty(key = "rowSize", value = "行数"),
                    @ApiJsonProperty(key = "type", value = "类型")
            }) @RequestBody JSONObject jsonParam) {

        try {
            String account = jsonParam.getString("account");
            Asserts.notBlank(account, "用户名");
            Integer pageSize = jsonParam.getInteger("pageSize");
            Integer rowSize = jsonParam.getInteger("rowSize");
            Integer type = jsonParam.getInteger("type");
            Asserts.naturalNumb(type, "类型");
            if (pageSize==null|| "".equals(pageSize)){
                pageSize = 0;
            }
            if (rowSize == null || "".equals((rowSize))){
                rowSize = 10;
            }
            List<TransactionVo>  transactionVos = goodsService.tranRecord(account,type,pageSize,rowSize);
            return ResponseWrapper.succeed(transactionVos);
        }catch (ApiException e){
            log.error(String.format("[前端]获取明细列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]获取明细列表列表"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @RequestMapping(value = "/OrderReceiver", method = RequestMethod.POST)
    @ApiOperation(tags = "商品", httpMethod = "POST", value = "[前端]确认收货", response = String.class)
    public ResponseWrapper OrderReceiver(
            @ApiJsonObject(name = "OrderReceiverParams", value = {
                    @ApiJsonProperty(key = "orderId", value = "orderId")
            }) @RequestBody JSONObject jsonParam) {

        try {
            String account = jsonParam.getString("account");
            Asserts.notBlank(account, "用户名");
            Integer orderId = jsonParam.getInteger("orderId");
            Asserts.naturalNumb(orderId, "订单ID");
            goodsService.OrderReceiver(orderId);
            return ResponseWrapper.succeed();
        }catch (ApiException e){
            log.error(String.format("[前端]确认收货失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]确认收货失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
